package com.example.tests;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.builder.CompareToBuilder;
import com.example.utils.SortedListOf;

public class ContactDataPhone implements Comparable<ContactDataPhone>{
	
	public String name;
	private String home;
	private String mobile;
	private String work;

	public ContactDataPhone() {
	}

	@Override
	public String toString() {
		return "ContactDataPhone [name=" + name + ", home=" + home + ", mobile=" + mobile
				+ ", work=" + work + "]";
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ContactDataRow other = (ContactDataRow) obj;
		return this.toString().equals(other.toString());
	}

	@Override 
	public int compareTo(ContactDataPhone other) {
		return new CompareToBuilder()
		.append(this.name, other.name)
		.append(this.home, other.home)
		.append(this.work, other.work)
		.append(this.mobile, other.mobile)
        .toComparison();
	}
	
	public ContactDataPhone withName(String name) {
		this.name = name;
		return this;
	}
	
	public ContactDataPhone withHome(String phone) {
		this.home = phone;
		return this;
	}
	
	public ContactDataPhone withWork(String phone) {
		this.work = phone;
		return this;
	}
	
	public ContactDataPhone withMobile(String phone) {
		this.mobile = phone;
		return this;
	}
	
	public String getName() {
		return name;
	}

	public String getHome() {
		return home;
	}

	public void setHome(String home) {
		this.home = home;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getWork() {
		return work;
	}

	public void setWork(String work) {
		this.work = work;
	}

	public void setName(String name) {
		this.name = name;
	}

	public static void assertPrintContactsEqual(
			SortedListOf<ContactDataPhone> expected, 
			SortedListOf<ContactData> actualContactList) 
	{
		assertThat("Contacts list have different sizes", expected.size(), equalTo(actualContactList.size()));
		SortedListOf<ContactDataPhone> actual = new SortedListOf<ContactDataPhone>(transformContactList(actualContactList));
		for (int i = 0; i < expected.size(); i++) 
		{
			ContactDataPhone exp = expected.get(i);
			ContactDataPhone act = actual.get(i);
			System.out.println("Expected : " + exp);
			System.out.println("Actual   : " + act);
			assertThat("Different Names", exp.getName(), equalTo((act.getName())));
			assertThat("Different Home phones", exp.getHome(), equalTo(act.getHome()));
			assertThat("Different Work phones", exp.getWork(), equalTo(act.getWork()));
			assertThat("Different Mobile phones", exp.getMobile(), equalTo(act.getMobile()));
		}
	}
	
	public static List<ContactDataPhone> transformContactList(List<ContactData> contactList) {
		List<ContactDataPhone> result = new ArrayList<ContactDataPhone>();
		for (ContactData cd : contactList) {
			ContactDataPhone ph = new ContactDataPhone()
			    .withName((cd.getFirstname() + " " + cd.getLastname()).trim())
			    .withHome(cd.getHome())
			    .withWork(cd.getWork())
			    .withMobile(cd.getMobile());
			result.add(ph);
		}
		return result;
	}
}